package com.mck.controller;

import com.mck.service.AiReactiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import org.springframework.http.codec.ServerSentEvent;

@RestController
@RequestMapping("/api/ai")
public class AiReactiveController {

  @Autowired
  private AiReactiveService aiService;

  @GetMapping(value = "/chat-reactive", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
  public Flux<ServerSentEvent<String>> chatReactive(@RequestParam String message) {
    return aiService.processAiRequestReactive(message)
        .map(content -> ServerSentEvent.<String>builder()
            .event("message")
            .data(content)
            .build());
  }
}